<template>
  <div class="sub-filter" v-if="filterData && !jiazaiPanduan">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <!-- 品牌详情数据 -->
        <a  @click="changeBrand(brand.id)" :class="{active:filterData.selectedBrand===brand.id}" v-for="brand in filterData.brands" :key="brand.id">{{brand.name}}</a>
      </div>
    </div>
    <!-- 规格数据详情 -->
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{p.name}}：</div>
      <div class="body">
        <!-- 规格详情 -->
        <a @click="changeAttr(p,attr.id)" :class="{active:p.selectedProp===attr.id}" v-for="attr in p.properties" :key="attr.id">{{attr.name}}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XTXGuJia class="item" width="800px" height="40px"  />
    <XTXGuJia class="item" width="800px" height="40px"  />
    <XTXGuJia class="item" width="600px" height="40px"  />
    <XTXGuJia class="item" width="600px" height="40px"  />
    <XTXGuJia class="item" width="600px" height="40px"  />
  </div>
</template>
<script>
import { erjifenleiqinqiu } from '@/Api/index'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    // 默认选中全部
    const route = useRoute()
    const filterData = ref(null)
    const jiazaiPanduan = ref(false)
    //  因路由的关系 分类发生变化的时候并不会刷新数据 需要监听进行重新获取筛选数据
    watch(() => route.params.id, (idx) => {
      // 判端是否是二级类目下的路径，因为点击一级类目也会启动监听器
      if (idx && route.path === (`/FenLei/ErjiZhanshi/${idx}`)) {
        jiazaiPanduan.value = true
        // 请求数据
        idx && erjifenleiqinqiu(route.params.id).then(({ result }) => {
          result.selectedBrand = null
          // 品牌 全部 数据 存入请求数据中
          result.brands.unshift({ id: null, name: '全部' })
          // 销售属性 全部 数据 存入请求数据中
          result.saleProperties.forEach(p => {
            p.selectedProp = undefined
            p.properties.unshift({ id: null, name: '全部' })
          })
          filterData.value = result
          jiazaiPanduan.value = false
        })
      }
    }, { immediate: true })

    // 获取筛选参数
    const getFilterParams = () => {
      const filterParams = {}
      const attrs = []
      filterParams.brandId = filterData.value.selectedBrand
      filterData.value.saleProperties.forEach(p => {
        const attr = p.properties.find(attr => attr.id === p.selectedProp)
        if (attr && attr.id !== undefined) {
          attrs.push({ groupName: p.name, propertyName: attr.name })
        }
      })
      if (attrs.length) filterParams.attrs = attrs
      return filterParams
    }

    // 选择品牌
    const changeBrand = (brandId) => {
      if (filterData.value.selectedBrand === brandId) return
      filterData.value.selectedBrand = brandId
      emit('paixu', getFilterParams())
    }
    // 选中属性
    const changeAttr = (p, attrId) => {
      if (p.selectedProp === attrId) return
      p.selectedProp = attrId
      emit('paixu', getFilterParams())
    }
    return { filterData, jiazaiPanduan, changeBrand, changeAttr }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    width: 1240px;
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
    .xtx-skeleton {
      padding: 10px 0;
    }
</style>
